/******************************************************************************
 * Copyright (c) 2014 - 2016. Danlu.com Co.Ltd. All rights reserved.          *
 ******************************************************************************/

package cn.com.dhc.danlu.widget.couponview;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;

import cn.com.dhc.danlu.R;
import cn.com.dhc.danlu.util.CommonConst.CouponConst;
import cn.com.dhc.danlu.widget.expandablelayout.ExpandableLayout;
import cn.com.dhc.danlu.widget.textview.CustomFontTextView;

/**
 * @author: 李一方
 * @author: 吴海洋
 * @version: 1.0.0
 * @since: 2016-11-07 04:18
 * @mail: liyifang@danlu.com
 * @desc: 优惠券、红包的视图基类
 */
public abstract class BaseDanluCouponView extends FrameLayout {
    protected TextView tvUnit;
    protected TextView tvPrice;
    protected TextView tvQuota;
    protected TextView tvRestrict;
    protected TextView tvLimitDetail;
    protected TextView tvActiveTime;
    protected ImageView ivLimit;
    protected ImageView ivUseState;
    protected FrameLayout flOrder;
    protected SawtoothView svRight;
    protected ExpandableLayout elLimit;
    protected CustomFontTextView cftAction;
    protected LinearLayout llRight;
    private OnRightItemClick onRightItemClick;

    public BaseDanluCouponView (Context context) {
        this (context, null);
    }

    public BaseDanluCouponView (Context context, AttributeSet attrs) {
        this (context, attrs, 0);
    }

    public BaseDanluCouponView (Context context, AttributeSet attrs, int defStyleAttr) {
        super (context, attrs, defStyleAttr);
        initGeneralView (LayoutInflater.from (getContext ()).inflate (getViewLayoutId (), this, true));
        bindView ();
    }

    private void initGeneralView (View root) {
        setBackgroundColor (Color.WHITE);
        elLimit = (ExpandableLayout) root.findViewById (R.id.el_limit);
        tvUnit = (TextView) root.findViewById (R.id.tv_unit);
        tvPrice = (TextView) root.findViewById (R.id.tv_price);
        tvQuota = (TextView) root.findViewById (R.id.tv_quota);
        tvRestrict = (TextView) root.findViewById (R.id.tv_restrict);
        tvLimitDetail = (TextView) root.findViewById (R.id.tv_limit_detail);
        tvActiveTime = (TextView) root.findViewById (R.id.tv_active_time);
        cftAction = (CustomFontTextView) root.findViewById (R.id.cft_action);
        ivLimit = (ImageView) root.findViewById (R.id.iv_limit);
        svRight = (SawtoothView) root.findViewById (R.id.sv_right);
        ivUseState = (ImageView) root.findViewById (R.id.iv_use_state);
        flOrder = (FrameLayout) root.findViewById (R.id.fl_order);
        llRight = (LinearLayout) root.findViewById (R.id.ll_right);
        initChildView (root);
    }

    protected abstract int getViewLayoutId ();

    private void bindView () {
        flOrder.setOnClickListener (new OnClickListener () {
            @Override
            public void onClick (View v) {
                if (null != onRightItemClick) {
                    onRightItemClick.onRightItemClick (v, flOrder.getId ());
                }
            }
        });
        svRight.setOnClickListener (new OnClickListener () {
            @Override
            public void onClick (View v) {
                if (null != onRightItemClick) {
                    onRightItemClick.onRightItemClick (v, svRight.getId ());
                }
            }
        });
    }

    protected void initChildView (View root) {}

    public void setPrice (String price) {
        // 当数字达到六位长度时，有些拥挤，这里需要调整下字号
        int[] textSize = new int[2];
        if (isYhq ()) {
            textSize[0] = 20;
            textSize[1] = 22;
        } else {
            textSize[0] = 22;
            textSize[1] = 24;
        }
        if (price.length () > 4) {
            tvPrice.setTextSize (TypedValue.COMPLEX_UNIT_SP, textSize[0]);
            tvUnit.setTextSize (TypedValue.COMPLEX_UNIT_SP, textSize[0] - 2);
        } else {
            tvPrice.setTextSize (TypedValue.COMPLEX_UNIT_SP, textSize[1]);
            tvUnit.setTextSize (TypedValue.COMPLEX_UNIT_SP, textSize[1] - 2);
        }
        if (price.equals ("优惠券") || price.equals ("红包")) {
            tvUnit.setVisibility (GONE);
            tvPrice.setTypeface (Typeface.DEFAULT);
        } else {
            tvUnit.setVisibility (VISIBLE);
            tvPrice.setTypeface (Typeface.DEFAULT_BOLD);
        }
        tvPrice.setText (price);
    }

    protected abstract boolean isYhq ();

    public void setOnExpandListener (ExpandableLayout.OnExpandListener onExpandListener) {
        elLimit.setOnExpandListener (onExpandListener);
    }

    public void setActiveTime (String activeTime) {
        if (TextUtils.isEmpty (activeTime)) {
            tvActiveTime.setVisibility (GONE);
        } else {
            tvActiveTime.setText (activeTime);
        }
    }

    public void setLimitDetail (List<String> limitDetails) {
        tvLimitDetail.setText (getOrderListSpan (limitDetails));
    }

    private SpannableStringBuilder getOrderListSpan (List<String> list) {
        SpannableStringBuilder msp = new SpannableStringBuilder ();
        for (int i = 0; i < list.size (); i++) {
            int contentStart = msp.length ();
            msp.append (list.get (i)).append ("\n");
            msp.setSpan (new NumberIndentSpan (25, 30, (i + 1)), contentStart, msp.length (), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        return msp;
    }

    public void setRestrict (String restrict) {
        tvRestrict.setText (restrict);
    }

    public void setQuota (String quota) {
        tvQuota.setText (quota);
    }

    public void setSelect (boolean isSelect) {
        ivLimit.setSelected (isSelect);
    }

    public void wrapGetStatus (String status) {
        switch (status) {
            case CouponConst.DEALERS_NOT_USE:
                setGetState (CouponGetState.getNow);
                break;

            case CouponConst.DEALERS_HAS_USED:
                setGetState (CouponGetState.getOver);
                break;

            case CouponConst.DEALERS_OUT_DATE:
                setGetState (CouponGetState.grabOver);
                break;
        }
    }

    protected abstract void setGetState (CouponGetState grabOver);

    public void setUseState (CouponUseState state) {
        switch (state) {
            case usedOver:
                setIvUseStateVisible (VISIBLE);
                ivUseState.setImageResource (R.drawable.icon_coupon_usedover);
                handleOtherUseSituation (state);
                break;

            case dateOver:
                setIvUseStateVisible (VISIBLE);
                ivUseState.setImageResource (R.drawable.icon_coupon_dateover);
                handleOtherUseSituation (state);
                break;

            case usedNot:
                setIvUseStateVisible (GONE);
                handleOtherUseSituation (state);
                break;
        }
    }

    public void setIvUseStateVisible(int visible){
        ivUseState.setVisibility (visible);
    }

    /**
     * dateOver & usedNot
     */
    protected abstract void handleOtherUseSituation (CouponUseState state);

    protected void setLimitRightPadding (int padding) {
        tvActiveTime.setPadding (0, 0, padding, 0);
    }

    public void setExpand (boolean expand) {
        elLimit.setExpand (expand); 
        ivLimit.setSelected (expand);
    }

    public void setRightActionIsVisible (int svRightVisible, int flOrderVisible) {
        if (svRightVisible == VISIBLE || flOrderVisible == VISIBLE) {
            llRight.setVisibility (VISIBLE);
        }
        svRight.setVisibility (svRightVisible);
        flOrder.setVisibility (flOrderVisible);
    }

    public void setOnRightItemClick (OnRightItemClick onRightItemClick) {
        this.onRightItemClick = onRightItemClick;
    }
}